package top.iridescent.kafka;


import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class KafkaConsumer2 {
    // 消费监听
    @KafkaListener(topics = {"test2"},groupId = "g2")
    public void onMessage1(ConsumerRecord<?, ?> record){
        // 消费的哪个topic、partition的消息,打印出消息内容
        System.out.println("消费者2-1简单消费:  topic："+record.topic()+"-partition: "+record.partition()+"- value: "+record.value());
    }

    // 消费监听
    @KafkaListener(topics = {"test2"},groupId = "g2")
    public void onMessage2(ConsumerRecord<?, ?> record){
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            //log.info("-xiaofeng2-- test2：" + record+"，record:" + record+",message:" + message+",---");
        }

        // 消费的哪个topic、partition的消息,打印出消息内容
        System.out.println("消费者2-2简单消费：topic："+record.topic()+"-partition: "+record.partition()+"- value: "+record.value());
    }
}
